/**
 * Created by xu on 2017/1/3.
 */
$(function () {
    var userId=$("#userId").val();
    if(userId==null || userId==undefined || userId==''){
        alert("用户没有登录！");
        return;
    }

getShoppingCartList();
});

function getShoppingCartList() {
    var postData={};
    $.ajax({
        type: 'post',
        url: '/order/getShoppingCartList.html',
        data:postData,
        success: function (data) {

        }
    });
}

getCount();
//提交数据
$("#cart-pay").click(function () {
    var number = $("#buyNum").val();
    if (number > 10 || number < 1) {
        alert("你输入的课程数量必须在1-10之间");
        return;
    }

    var classIds = "";
    var productJsons = [];

    $(".order-list-main").each(function () {

        var productJson = {classId: $(this).attr("data-classId"), buyNum: $(this).find("input[type=number]").val()};
        productJsons.push(productJson);

    });
    var activityIds = $("#activityIds").val().split(",");
    console.log(activityIds)
    var activityList = [];

    for (var i = 0; i < activityIds.length; i++) {


        if (activityIds[i] =="" ||  activityIds[i] == null || activityIds[i] == undefined) {
            alert(activityId);
            continue;
        }
        var activityId = {activityId: activityIds[i]};
        activityList.push(activityId)
    }
    createOrder(1, productJsons, true, 1, activityList);
    // createOrder(productJsons);
});
function getCount() {
    var price = 0;
    var count = 0;
    $(".order-list-main").each(function () {
        price += parseFloat($(this).attr("data-price") / 100) * $(this).find("input[type=number]").val();

        $("#cart-total").text(price.toFixed(2));
    });
}
function clearShoppingCart() {
    $.ajax({
        type: 'get',
        url: '/clearShoppingCart',
        success: function (data) {
            if (data.code != 1) {
                alert("清空购物车失败");
                return;
            }
            alert("清空购物车成功");
            loadDiv("/customerCenter/shoppingCart");
        }
    });
}

function cancelItem(shoppingCartId) {
    $.ajax({
        type: 'get',
        url: '/reduceShoppingCart?shoppingCartId=' + shoppingCartId,
        success: function (data) {
            loadDiv("/customerCenter/shoppingCart", 3);
        }
    });
}
function modifyCart(shoppingCartId, obj) {
    var productCount = obj.val();
    var data = {shoppingCartId: shoppingCartId, productCount: productCount};
    $.ajax({//获取用户id

        type: 'post',
        url: '/modifyCart',
        data: data,
        success: function (data) {
            /*        var data = JSON.parse(json); //由JSON字符串转换为JSON对象
             console.log(data);*/
            if (data.code != 1) {
                alert(data.msg);
                return;
            }
            getCount();

        }
    });
}



